package com.zhanggm.springcloud.student.service;

import com.zhanggm.springcloud.student.dao.*;
import com.zhanggm.springcloud.student.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentServie{
    @Autowired
    private ClassesRepository classesRepository;
    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private AreaRepository areaRepository;

    @Autowired
    private HobbyRepository hobbyRepository;

    @Autowired
    private StudentHobbyRepository studentHobbyRepository;

    @Override
    public ClassesEntiy addClasses(ClassesEntiy classesEntiy) {
        return classesRepository.save(classesEntiy);
    }

    @Override
    public List<ClassesEntiy> getClassesAll() {
        return classesRepository.findAll();
    }

    @Override
    @Transactional
    public StudentEntiy save(StudentEntiy studentEntiy) {
        //保存学生信息
        studentEntiy.setCreateTime(new Date());
        StudentEntiy student = studentRepository.save(studentEntiy);
        //删除关系表的数据
        studentHobbyRepository.deleteByStudentId(student.getId());
        //添加关系表数据
        Long id = student.getId();
        List<Long> hobbyIdList = studentEntiy.getHobbyIdList();
        if(hobbyIdList!=null && hobbyIdList.size()>0){
            hobbyIdList.forEach(hobbyId->{
                StudentHobbyEntiy studentHobbyEntiy = new StudentHobbyEntiy(id,hobbyId);
                studentHobbyRepository.save(studentHobbyEntiy);
            });
        }
        return student;
    }

    @Override
    public Page<StudentEntiy> getPageInfo(StudentEntiy studentEntiy, Integer pageNum, Integer pageSize) {
        Specification<StudentEntiy> specification = new Specification<StudentEntiy>() {
            @Override
            public Predicate toPredicate(Root<StudentEntiy> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                if(studentEntiy.getName()!=null){
                    Predicate predicate = cb.like(root.get("name"), "%".concat(studentEntiy.getName().concat("%")));
                    predicates.add(predicate);
                }
                if(studentEntiy.getClassesId()!=null){
                    Predicate predicate = cb.equal(root.get("classesId"), studentEntiy.getClassesId());
                    predicates.add(predicate);
                }

                if(studentEntiy.getClassesName()!=null){
                    Predicate classesNamelike = cb.like(root.get("classes").get("name"), "%".concat(studentEntiy.getClassesName().concat("%")));
                    predicates.add(classesNamelike);
                }

                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Pageable pageable = PageRequest.of(pageNum-1,pageSize, Sort.Direction.DESC,"createTime");
        return studentRepository.findAll(specification,pageable);
    }

    @Override
    public boolean deleteById(Long id) {
        studentRepository.deleteById(id);
        return true;
    }

    @Override
    public boolean deleteByIds(String ids) {
        return studentRepository.deleteByIds(ids)>0;
    }

    @Override
    public boolean deleteByIds(List<Long> ids) {
        List<StudentEntiy> delStus = studentRepository.findAllById(ids);
        studentRepository.deleteInBatch(delStus);
        return true;
    }

    @Override
    public StudentEntiy getById(Long id) {
        return studentRepository.findById(id).get();
    }

    @Override
    public List<AreaEntiy> getAreaListByPid(Long pid) {
        return areaRepository.findByPid(pid);
    }

    @Override
    public List<AreaEntiy> getAreaTree() {
        /** 查询地区表所有数据 **/
        List<AreaEntiy> all = areaRepository.findAll();
        /** 查询省列表 **/
        List<AreaEntiy> proviceList = areaRepository.findByPid(1L);
        proviceList.forEach(provice->{
            List<AreaEntiy> cityList = new ArrayList<>();
            /** 查询省下面的城市**/
            all.forEach(city->{
                if(city.getPid().intValue()==provice.getId().intValue()){
                    cityList.add(city);
                    /** 查询市下面的地区**/
                    List<AreaEntiy> areaList = new ArrayList<>();
                    all.forEach(area->{
                        if(area.getPid().intValue()==city.getId().intValue()){
                            areaList.add(area);
                        }
                    });
                    city.setSubAreaList(areaList);
                }
            });
            provice.setSubAreaList(cityList);
        });
        return proviceList;
    }

    @Override
    public List<HobbyEntiy> getHobbyAll() {
        return hobbyRepository.findAll();
    }

    @Override
    public List<Long> getHobbyIdListByStudentId(Long studentId) {
        return studentHobbyRepository.findByStudentId(studentId);
    }
}
